﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StockMonitor.Helper;
using System.Windows.Forms;
using CLFramework.Utility;

namespace StockMonitor
{
    /// <summary>
    /// 用户身份信息管理
    /// </summary>
    /// <remarks>
    /// 代码编写：<author>LANCEZHANG-PC\zhangxl</author><br/>
    /// 编写时间：<datetime>2012-10-16 5:04 PM</datetime>
    /// </remarks>
    public class AuthUserContext
    {
        #region Members

        public static AuthUserContext Instance = new AuthUserContext();

        private AuthUserContext()
        {
            if (!hs.Connect("Configs\\prmNetwork.ini"))
            {
                //MessageBox.Show("交易接口连接失败");
                Log.Error("交易接口连接失败");
            }
        }

        HsGateway hs = new HsGateway();

        #endregion

        #region Properties

        /// <summary>
        /// 资产帐号
        /// </summary>
        public string FundAccount { get; set; }

        /// <summary>
        /// 密码
        /// </summary>
        public string Password { get; set; }

        /// <summary>
        /// 客户姓名
        /// </summary>
        public string ClientName { get; set; }

        /// <summary>
        /// 资金类型
        /// </summary>
        public string MoneyType { get; set; }

        /// <summary>
        /// 当前余额
        /// </summary>
        public double CurrentBalance { get; set; }

        /// <summary>
        /// 可用资金
        /// </summary>
        public double EnableBalance { get; set; }

        /// <summary>
        /// 可取现金
        /// </summary>
        public double FetchCash { get; set; }

        /// <summary>
        /// 资产总值
        /// </summary>
        public double AssetBalance { get; set; }

        /// <summary>
        /// 上海股东帐号
        /// </summary>
        public string SHStockAccount { get; set; }

        /// <summary>
        /// 深圳股东帐号
        /// </summary>
        public string SZStockAccount { get; set; }

        #endregion

        /// <summary>
        /// 登陆，使用【F200客户登录校验】登陆资金帐户。
        /// 并使用【F405查询资金】查询资金。
        /// 使用【F407查询股东】查询沪深股票帐号。
        /// </summary>
        /// <param name="account">用户名.</param>
        /// <param name="pwd">密码.</param>
        /// <returns></returns>
        /// <remarks>
        /// 代码编写：<author>LANCEZHANG-PC\zhangxl</author><br/>
        /// 编写时间：<datetime>2012-10-16 5:04 PM</datetime>
        /// </remarks>
        public bool Login(string account, string pwd)
        {
            try
            {
                var result = hs.F200客户登录校验(HsGateway.op_branch_no, HsGateway.branch_no, "1", pwd, "0", account);

                Instance = new AuthUserContext();

                Instance.FundAccount = account;
                Instance.Password = pwd;
                Instance.ClientName = result.client_name;
                Instance.MoneyType = result.money_type == "0" ? "人民币" : result.money_type == "1" ? "美元" : "港币";

                var result2 = hs.F405查询资金(HsGateway.op_branch_no, HsGateway.branch_no, result.fund_account, pwd, "0");
                Instance.CurrentBalance = result2.First().current_balance;
                Instance.EnableBalance = result2.First().enable_balance;
                Instance.FetchCash = result2.First().fetch_cash;
                Instance.AssetBalance = result2.First().asset_balance;

                var result3 = hs.F407查询股东(HsGateway.op_branch_no, HsGateway.branch_no, result.fund_account, pwd, "", "", "", 5, "");
                Instance.SHStockAccount = result3.Find(f => f.exchange_type == "1") != null ? result3.Find(f => f.exchange_type == "1").stock_account : string.Empty;
                Instance.SZStockAccount = result3.Find(f => f.exchange_type == "2") != null ? result3.Find(f => f.exchange_type == "2").stock_account : string.Empty;

                return true;
            }
            catch (HsGateway.HsCommException hsex)
            {
                MessageBox.Show("接口错误U02：" + hsex.ErrorInfo.ToString());
                return false;
            }
            catch (System.Net.WebException wex)
            {
                MessageBox.Show("网络错误U02：" + wex.Message);
                return false;
            }
            catch (Exception ex)
            {
                MessageBox.Show("未知错误U02：" + ex.Message);
                return false;
            }
        }

        /// <summary>
        /// 查询所持沪、深股票仓位.
        /// </summary>
        /// <returns>持仓列表</returns>
        /// <remarks>
        /// 代码编写：<author>LANCEZHANG-PC\zhangxl</author><br/>
        /// 编写时间：<datetime>2012-10-16 5:07 PM</datetime>
        /// </remarks>
        public List<HsGateway.ResultF403> QueryStockPosistion()
        {
            try
            {
                int fundAccount;
                int.TryParse(FundAccount, out fundAccount);

                List<HsGateway.ResultF403> result = hs.F403查询股票(
                    HsGateway.op_branch_no
                    , HsGateway.branch_no
                    , fundAccount
                    , Password
                    , string.Empty
                    , SHStockAccount
                    , string.Empty
                    , string.Empty
                    , "0"
                    , 1000
                    , string.Empty);

                result.AddRange(hs.F403查询股票(
                    HsGateway.op_branch_no
                    , HsGateway.branch_no
                    , fundAccount
                    , Password
                    , string.Empty
                    , SZStockAccount
                    , string.Empty
                    , string.Empty
                    , "0"
                    , 1000
                    , string.Empty));

                return result;
            }
            catch (HsGateway.HsCommException hsex)
            {
                //MessageBox.Show("错误：" + hsex.ErrorInfo.ToString());

                Log.Exception("接口错误U01", hsex);
                return null;
            }
            catch (System.Net.WebException wex)
            {
                //MessageBox.Show("网络错误：" + wex.Message);

                Log.Exception("网络错误U01", wex);
                return null;
            }
            catch (Exception ex)
            {
                //MessageBox.Show("未知错误：" + ex.Message);

                Log.Exception("未知错误U01", ex);
                return null;
            }
        }

        /// <summary>
        /// 刷新资金帐户余额.
        /// </summary>
        /// <returns>是否成功</returns>
        /// <remarks>
        /// 代码编写：<author>LANCEZHANG-PC\zhangxl</author><br/>
        /// 编写时间：<datetime>2012-10-16 5:08 PM</datetime>
        /// </remarks>
        public bool RefreshBalance()
        {
            try
            {
                var result = hs.F405查询资金(HsGateway.op_branch_no, HsGateway.branch_no, int.Parse(FundAccount), Password, "0");
                if (result == null)
                    return false;

                //update data.
                Instance.CurrentBalance = result.First().current_balance;
                Instance.EnableBalance = result.First().enable_balance;
                Instance.FetchCash = result.First().fetch_cash;
                Instance.AssetBalance = result.First().asset_balance;

                return true;
            }
            catch
            {
                throw;
            }
        }
    }
}
